﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;

namespace Dzzzr
{
	public partial class DetailFormImageEditor : Form
	{
		public DetailFormImageEditor()
		{
			InitializeComponent();
		}

		/// <summary>
		/// Image stream
		/// </summary>
		public MemoryStream ImageStream = new MemoryStream();

		/// <summary>
		/// Load image
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnLoad_Click(object sender, EventArgs e)
		{
			try
			{
				OpenFileDialog openFileDialog = new OpenFileDialog();
				if (openFileDialog.ShowDialog() == DialogResult.OK)
				{
					Stream newStream = openFileDialog.OpenFile();
					if (newStream != null)
					{
						using (newStream)
						{
							// show image
							PictureBox.Image = new Bitmap(newStream);

							// read bytes
							newStream.Seek(0, SeekOrigin.Begin);
							byte[] data = new byte[newStream.Length];
							newStream.Read(data, 0, data.Length);

							// save bytes
							ImageStream.SetLength(data.Length);
							ImageStream.Seek(0, SeekOrigin.Begin);
							ImageStream.Write(data, 0, data.Length);
						}
					}
				}
			}
			catch (Exception ex)
			{
				Debug.WriteLine(ex.ToString());
				MessageBox.Show(ex.ToString());
			}
		}

		/// <summary>
		/// Form loading
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void DetailFormImageEditor_Load(object sender, EventArgs e)
		{
			try
			{
				if (ImageStream.Length > 0)
				{
					ImageStream.Seek(0, SeekOrigin.Begin);
					PictureBox.Image = new Bitmap(ImageStream);
				}
			}
			catch (Exception ex)
			{
				Debug.WriteLine(ex.ToString());
				MessageBox.Show(ex.ToString());
			}
		}

		/// <summary>
		/// Clear image
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnClear_Click(object sender, EventArgs e)
		{
			ImageStream.SetLength(0);
			PictureBox.Image = null;
		}
	}
}
